function checkAlgorithmInsert(){
    var descr=document.getElementById("description").value
    var source=document.getElementById("source").value
    var descrErr=document.getElementById("descrErr")
    var sourceErr=document.getElementById("sourceErr")
    descrErr.innerHTML="";
    sourceErr.innerHTML="";
    var ret=true;
    if(descr==''){
        descrErr.innerHTML="You must fill this field";
        ret=false;
    }
    if(source==''){
        sourceErr.innerHTML="You must fill this field";
        ret=false;
    }
    if(descr.indexOf("<")>-1 || descr.indexOf("&")>-1 || descr.indexOf("\"")>-1){
        descrErr.innerHTML="It is forbidden to insert the characters &lt; &quot; and &amp; in the description";
        ret=false;
    }
    return ret;
}

function checkCommentInsert(){
    var comment=document.getElementById("comment").value
    var err=document.getElementById("error");
    err.innerHTML="";
    var ret=true;
    if(comment==''){
        err.innerHTML="You have to write something in the comment";
        ret=false;
    }
    return ret;
}

function checkLogin(){
    var email=document.getElementById("email").value
    var pass=document.getElementById("pass").value
    var emailErr=document.getElementById("emailErr");
    emailErr.innerHTML="";
    var ret=true;
    if(email=='' || pass==''){
        alert("You have to insert both the parameters");
        ret=false;
    }
    if(ret){
            if(!checkEmail(email)){
                ret=false;
                emailErr.innerHTML="Email not valid";
            }
        }
    /*if(ret){
        location.reload(true);
        ret=false;
        if (window.XMLHttpRequest){
            xhttp=new XMLHttpRequest();
        }
        else{ // IE 5/6
            xhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xhttp.open("GET","../xml/Users.xml",false);
        xhttp.send();
        xmlDoc=xhttp.responseXML;
    
        var root = xmlDoc.documentElement;
        nodes = root.getElementsByTagName('user');
        var check=0; //0 doesn't exists, 1 wrong password, 2 login correct;
        for (i=0; i < nodes.length; i++) {
            if(nodes[i].getElementsByTagName('email')[0].childNodes[0].nodeValue==email){
                if(nodes[i].getElementsByTagName('password')[0].childNodes[0].nodeValue==encryptedPassword){
                    check=2;
                    ret=true;
                }
                else{
                    check=1;
                    passErr.innerHTML="Wrong password for the user";
                }
            }
        }
        if(check==0){
            emailErr.innerHTML="User "+email+" does not exists";
        }
    }*/
    return ret;
}

function checkEmail(email) {
    var emailErr=document.getElementById("emailErr");
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(email)) {
        emailErr.innerHTML="Please provide a valid email address";
        return false;
    }
    return true;
}

function checkRegistration(){
    var email=document.getElementById("email").value
    var conf_email=document.getElementById("conf_email").value
    var pass=document.getElementById("pass").value
    var conf_pass=document.getElementById("conf_pass").value
    var name=document.getElementById("name").value
    var surname=document.getElementById("surname").value
    var username=document.getElementById("username").value
    var country=document.getElementById("country").value
    var org=document.getElementById("org").value
    var passErr=document.getElementById("passErr");
    var emailErr=document.getElementById("emailErr");
    var userErr=document.getElementById("userErr");
    var nameErr=document.getElementById("nameErr");
    nameErr.innerHTML="";
    passErr.innerHTML="";
    emailErr.innerHTML="";
    userErr.innerHTML="";
    var ret=true;
    var errorInEmail=false;
    var required=false;
    if(email=='' || conf_email=='' || pass=='' || conf_pass=='' || username=='' || org==''){
        alert("You have to enter all required parameters");
        ret=false;
        required=true;
    }
    if(!required){
        if(pass!=conf_pass){
            passErr.innerHTML="The two passwords have to be equals";
            ret=false;
        }
        if(email!=conf_email){
            emailErr.innerHTML="The two emails have to be equals";
            errorInEmail=true;
            ret=false;
        }
        else{
            if(!checkEmail(email)){
                ret=false;
            }
        }
        if(username.indexOf("<")>-1 || username.indexOf("&")>-1 || username.indexOf("£")>-1){
            userErr.innerHTML="It is forbidden to insert the characters &lt; &pound; and &amp; in the username";
            ret=false;
        }
        if(pass.indexOf("<")>-1 || pass.indexOf("&")>-1 || pass.indexOf("\"")>-1 || pass.indexOf("£")>-1){
            passErr.innerHTML="It is forbidden to insert the characters &lt; &quot; &pound; and &amp; in the password";
            ret=false;
        }
        if(name.indexOf("<")>-1 || name.indexOf("&")>-1 || name.indexOf("£")>-1 ||
           surname.indexOf("<")>-1 || surname.indexOf("&")>-1 || surname.indexOf("£")>-1){
            nameErr.innerHTML="It is forbidden to insert the characters &lt; &pound; and &amp; in name and surname";
            ret=false;
        }
       /* if (window.XMLHttpRequest){
            xhttp=new XMLHttpRequest();
        }
        else{ // IE 5/6
            xhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xhttp.open("GET","../xml/Users.xml",false);
        xhttp.send();
        xmlDoc=xhttp.responseXML;
    
        var root = xmlDoc.documentElement;
        nodes = root.getElementsByTagName('user');
        for (i=0; i < nodes.length; i++) {
            if(nodes[i].getElementsByTagName('email')[0].childNodes[0].nodeValue==email){
                if(!errorInEmail){
                    emailErr.innerHTML="User "+email+" already exists";
                }
                ret=false;
            }
            if(nodes[i].getElementsByTagName('username')[0].childNodes[0].nodeValue==username){
                userErr.innerHTML="User "+username+" already exists";
                ret=false;
            }
        }*/
    }
    return ret;
}